package com.guiji.template.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.guiji.common.annotation.RepeatSubmit;
import com.guiji.common.annotation.Log;
import com.guiji.common.core.controller.BaseController;
import com.guiji.common.core.domain.AjaxResult;
import com.guiji.common.enums.BusinessType;
import com.guiji.template.vo.TemplateVo;
import com.guiji.template.bo.TemplateQueryBo;
import com.guiji.template.bo.TemplateAddBo;
import com.guiji.template.bo.TemplateEditBo;
import com.guiji.template.service.ITemplateService;
import com.guiji.common.utils.poi.ExcelUtil;
import com.guiji.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 模板Controller
 *
 * @author 钟维澎（四月）
 * @date 2021-07-01
 */
@Api(value = "模板控制器", tags = {"模板管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/template/template")
public class TemplateController extends BaseController {

	private final ITemplateService iTemplateService;

	/**
	 * 查询模板列表
	 */
	@ApiOperation("查询模板列表")
	@PreAuthorize("@ss.hasPermi('template:template:list')")
	@GetMapping("/list")
	public TableDataInfo<TemplateVo> list(@Validated TemplateQueryBo bo) {
		return iTemplateService.queryPageList(bo);
	}

	/**
	 * 导出模板列表
	 */
	@ApiOperation("导出模板列表")
	@PreAuthorize("@ss.hasPermi('template:template:export')")
	@Log(title = "模板", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<TemplateVo> export(@Validated TemplateQueryBo bo) {
		List<TemplateVo> list = iTemplateService.queryList(bo);
		ExcelUtil<TemplateVo> util = new ExcelUtil<TemplateVo>(TemplateVo.class);
		return util.exportExcel(list, "模板");
	}

	/**
	 * 获取模板详细信息
	 */
	@ApiOperation("获取模板详细信息")
	@PreAuthorize("@ss.hasPermi('template:template:query')")
	@GetMapping("/{id}")
	public AjaxResult<TemplateVo> getInfo(@NotNull(message = "主键不能为空")
										  @PathVariable("id") Long id) {
		return AjaxResult.success(iTemplateService.queryById(id));
	}

	/**
	 * 新增模板
	 */
	@ApiOperation("新增模板")
	@PreAuthorize("@ss.hasPermi('template:template:add')")
	@Log(title = "模板", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@Validated @RequestBody TemplateAddBo bo) {
		return toAjax(iTemplateService.insertByAddBo(bo) ? 1 : 0);
	}

	/**
	 * 修改模板
	 */
	@ApiOperation("修改模板")
	@PreAuthorize("@ss.hasPermi('template:template:edit')")
	@Log(title = "模板", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping()
	public AjaxResult<Void> edit(@Validated @RequestBody TemplateEditBo bo) {
		return toAjax(iTemplateService.updateByEditBo(bo) ? 1 : 0);
	}

	/**
	 * 删除模板
	 */
	@ApiOperation("删除模板")
	@PreAuthorize("@ss.hasPermi('template:template:remove')")
	@Log(title = "模板" , businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
								   @PathVariable Long[] ids) {
		return toAjax(iTemplateService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
	}
}
